package com.example.testproj;

import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.testproj.suspend.SuspendActivity;
import com.example.testproj.view.ViewTestActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(TAG, "handleMessage: ");
        }
    };

    private int count = 0;
    private ServiceConnection serviceConnection;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        ViewGroup contentParent = (ViewGroup) findViewById(android.R.id.content);
//
//        contentParent.addView(LayoutInflater.from(this).inflate(R.layout.activity_main,contentParent));
        handler.sendEmptyMessage(0);
        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                return false;
            }
        });
        ((ViewGroup) getWindow().getDecorView()).getChildAt(0).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "onTouch: 我已经被处理了！");
                return true;
            }
        });
        button = (Button) findViewById(R.id.button);
        button.setClickable(false);
        button.setLongClickable(false);


        TextView textView = (TextView) findViewById(R.id.text1);
        TextView textView2 = (TextView) findViewById(R.id.text2);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getAction();
                final int actionMasked = action & MotionEvent.ACTION_MASK;
                if (event.getAction() != MotionEvent.ACTION_MOVE) {

                    Log.d(TAG, "onTouch: 我是textview1 " + actionMasked);
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    count = 2;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {

                    count = 1;
                    return true;
                }

                return true;
            }
        });

        textView2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN ) {
//                    Log.d(TAG, "onTouch: 我是textview2   ACTION_DOWN");
//                    return false;
//                }else if (event.getAction() == MotionEvent.ACTION_UP){
//                    Log.d(TAG, "onTouch: 我是textview2   ACTION_UP");
//                }
                if (event.getAction() != MotionEvent.ACTION_MOVE) {
                    final int action = event.getAction();
                    final int actionMasked = action & MotionEvent.ACTION_MASK;
                    Log.d(TAG, "onTouch: 我是textview2 " + actionMasked);
                }
                if (count == 2) {
                    return true;
                }
                return false;
            }
        });

//        if (Build.VERSION.SDK_INT >= 23) {
//            if (Settings.canDrawOverlays(this)) {
//                //有悬浮窗权限开启服务绑定 绑定权限
//                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
//                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
//                        0, 0, PixelFormat.TRANSPARENT);
//                layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
//                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
//                layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
//                layoutParams.x = 100;
//                layoutParams.y = 600;
//                WindowManager windowManager = getWindowManager();
//
//                Button button1 = new Button(this);
//                button1.setText("哈哈哈哈哈");
//                windowManager.addView(button1, layoutParams);
//
//
//            } else {
//                //没有悬浮窗权限m,去开启悬浮窗权限
//                try {
//                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
//                    startActivityForResult(intent, 100);
////                    if(Build.VERSION.*SDK_INT*>=23)
////                    {
////                        if(Settings.*canDrawOverlays*(this))
////                        {
////                            //有悬浮窗权限开启服务绑定 绑定权限
////                            Intent intent = new Intent(MainActivity.this, FBService.class);
////                            startService(intent);
////
////                        }else{
////                            //没有悬浮窗权限m,去开启悬浮窗权限
////
////
////                        }
////                    } else{
////                        //默认有悬浮窗权限  但是 华为, 小米,oppo等手机会有自己的一套Android6.0以下  会有自己的一套悬浮窗权限管理 也需要做适配
////                        Intent intent = new Intent(MainActivity.this, FBService.class);
////                        startService(intent);
////                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }

//         serviceConnection=  new ServiceConnection() {
//            @Override
//            public void onServiceConnected(ComponentName name, IBinder service) {
//                Log.d(TAG, "啊我连接了onServiceConnected: ");
//            }
//
//            @Override
//            public void onServiceDisconnected(ComponentName name) {
//                Log.d(TAG, "啊我断开了onServiceDisconnected: ");
//            }
//        };
//        bindService(new Intent(this, ServiceA.class), serviceConnection,BIND_AUTO_CREATE);
//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                Log.d(TAG, "run: 我运行在"+ Thread.currentThread().getName());
//            }
//        }.start();
        startActivity(new Intent(this, SuspendActivity.class));
        finish();
//        ArrayList list=new ArrayList();
//        list.add("1");
//        list.add("2");
//        list.add(1,"3");
//        list.get(2);
//        Log.d("qin",list.toString());
//
//
//        LinkedList linkedList=new LinkedList();
//        linkedList.add("1");
//        linkedList.add("2");
//        linkedList.add(1,"3");
//        linkedList.get(2);
//        Log.d("qin",linkedList.toString());

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ViewTestActivity.class));
//                finish();
            }
        });
        Log.d(TAG, "onCreate: "+button.getMeasuredHeight());
        button.post(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Runnable: "+button.getMeasuredHeight());
            }
        });


        printMaxMemory();
    }

    private void printMaxMemory() {
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        Log.d(TAG, "Max memory is " + maxMemory + "KB");
    }

    public void click(View view) {
        Log.d(TAG, "onclick: 我被点击了");
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: "+button.getMeasuredWidth());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: "+button.getMeasuredHeight());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: "+button.getMeasuredHeight());
      //  unbindService(serviceConnection);
    }
}
